<?php
namespace ac\controllers;

use Yii;
use ac\controllers\AuthController;
use app\models\Member;
use app\models\MemberSearch;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;


class MemberController extends AuthController
{

	/*列表*/
    public function actionMemberList()
    {
        if(isset($this->post['page'])){
            $currPage = intval($this->post['page']?$this->post['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $search = (new MemberSearch())->search($this->post);
        if($search === false){//验证搜索参数不通过
            $count = 0;
            $memberList = array();
        }else{
            $count = $search->count();
            $memberList = $search->select(['member_id', 'username', 'nickname', 'phone', 'amount', 'curr_integral', 'cont_sign_num', 'state', 'add_time'])
                                 ->offset($pageSize*($currPage-1))->limit($pageSize)
                                 ->orderBy(['add_time' => SORT_DESC])
                                 ->asArray()->all();
        }
        $res = array(
            'list' => $memberList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加*/
    public function actionAddMember()
    {
        if(Yii::$app->request->isPost and
            isset($this->post['Member']['comid']) and
            $this->checkComID($this->post['Member']['comid'])){
            // P($this->post);
            $member = new Member;
            if($member->addMember($this->post)){
                $backInfo = [0, '后台添加会员成功'];//返回信息
            }else{
                if($member->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $member->getErrors();//异常信息
                    if(array_key_exists("repeatUsername", $exception)){
                        $backInfo = ErrMap::getError(10011);//返回信息
                    };
                    if(array_key_exists("repeatPhone", $exception)){
                        $backInfo = ErrMap::getError(10014);//返回信息
                    };
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*
    修改
    $flag               值为setState时 作用是更改状态，为空是修改信息（更改状态的方法调用传参）
    $successBackInfo    成功的返回信息（更改会员状态的方法调用传参）
    */
    public function actionModifyMember($flag = '', $successBackInfo = '')
    {
        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost and
            isset($this->post['Member']['comid']) and
            $this->checkComID($this->post['Member']['comid'])){
            $id = isset($this->post['Member']['id'])?$this->post['Member']['id']:'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $member = new Member;
                if(empty($flag)){
                    $successBackInfo = '修改会员成功';//修改会员信息
                }
                if($member->modifyMember($this->post, $id, $flag)){
                    $backInfo = [0, $successBackInfo];//返回信息
                }else{
                    if($member->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $member->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $member = Member::find()
                           ->select(['member_id', 'username', 'phone', 'province', 'city', 'district', 'state'])
                           ->where('member_id = :id', [':id' => $id])->asArray()->one();
                if(!is_null($member)){
                    return Tool::showRes([0, '获取成功'], $member);
                }else{
                    return Tool::showRes(ErrMap::getError(10010));
                };
            }
        }

        return Tool::showRes(ErrMap::getError(10005));

    }


    /*（状态操作）*/
    public function actionSetState()
    {
        return $this->actionModifyMember('setState', '设置会员状态成功');
    }


    
}
